﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Uol.PagSeguro.Domain;
using AgileSaude.Business;
using AgileSaude.Entities;
using FBS.Framework;

namespace AgileSaude.Web
{
    public partial class Notificacao : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            try
            {
                string notificationType = Request.Form["notificationType"];
                sb.AppendLine(string.Format("notificationType = {0} <br/>", notificationType));

                string notificationCode = Request.Form["notificationCode"];
                sb.AppendLine(string.Format("notificationCode = {0} <br/>", notificationCode));

                Transaction transaction = new PagSeguroFacade(Server.MapPath("~/Configuration/PagSeguroConfig.xml")).ConsultaPorTransactionCode(notificationType, notificationCode);

                sb.AppendLine(string.Format("Reference = {0} <br/>", transaction.Reference));
                sb.AppendLine(string.Format("TransactionStatus = {0} <br/>", transaction.TransactionStatus));
                sb.AppendLine(string.Format("Code = {0} <br/>", transaction.Code));

                if (transaction != null)
                    new PedidoFacade().AtualizaStatus(transaction);

                sb.AppendLine(string.Format("Atualizado com sucesso!"));
                
            }
            catch (Exception ex)
            {
                sb.AppendLine(string.Format("Exception = {0}", ex.Message));
                if(ex.InnerException != null)
                    sb.AppendLine(string.Format("InnerException = {0}", ex.InnerException.Message));
            }
            finally
            {
                //Mail email = new Mail();
                //email.CorpoHTML = sb.ToString();
                //email.AdicionarDestinatario(TipoDestinatario.Para, "contato@agilesaude.com.br");
                //email.Assunto = "Notificação";
                //email.EnableSsl = false;
                //email.De = new System.Net.Mail.MailAddress("contato@agilesaude.com.br");

                //email.Enviar();

                //sb = null;
            }
        }
    }
}